package com.bookcode.controller;


import com.bookcode.entity.Course;
import com.bookcode.entity.Teacher;
import com.bookcode.service.TeacherService;
import com.bookcode.entity.HttpResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class TeacherController {

    @Autowired
    TeacherService teacherService;

    //通过姓名查找老师
    @GetMapping(value = "/teacher/name/{name}")
    public HttpResult findTeacherByName(@PathVariable String name){

        HttpResult<Teacher> result=new HttpResult<>();
//        System.out.println("name 是："+name);
//        System.out.println("findteacher: "+teacherService.findTeacherByName(name));
        result.setData(teacherService.findTeacherByName(name));
        return result;
    }
    //通过id查找老师
    @GetMapping(value = "/teacher/id/{id}")
    public HttpResult queryTeacherById(@PathVariable Long id){
         HttpResult<Teacher> result = new HttpResult<>();
         result.setData(teacherService.findTeacherById(id));
         result.setStatus("ok");
         return result;
    }
    //通过电话查找老师
    @GetMapping(value = "/teacher/gonghao/{gonghao}")
    public HttpResult findTeacherByGonghao(@PathVariable String gonghao){
        HttpResult<Teacher> result = new HttpResult<>();
        result.setData(teacherService.findTeacherByGonghao(gonghao));
        result.setStatus("ok");
        return result;
    }
    //通过电话查找老师
    @GetMapping(value = "/teacher/phone/{phone}")
    public HttpResult findTeacherPhone(@PathVariable String phone){
        HttpResult<Teacher> result = new HttpResult<>();
        result.setData(teacherService.findTeacherByPhone(phone));
        result.setStatus("ok");
        return result;
    }

    //查询所有老师
    @GetMapping(value = "/teachers")
    public HttpResult queryAll(){
        HttpResult<List<Teacher>> result = new HttpResult<>();
        result.setData(teacherService.findAll());
        result.setStatus("ok");
        return result;
    }


    /**
     *
     * 增加老师
     */
    @PostMapping(value = "/teachers")
    public HttpResult add(Teacher teacher) {
        HttpResult result = new HttpResult();
        try{
            teacherService.save(teacher);
            result.setStatus("save: "+teacher.toString()+" ok");
        }catch (Exception e){
            result.setStatus("wrong");
        }finally {
            return result;
        }

    }
    //删除
    @GetMapping(value="/delete/gonghao/{gonghao}")
    public HttpResult deleteTeacherByGonghao(@PathVariable("gonghao") String gonghao){
        System.out.println("工号： "+gonghao);
        HttpResult result=new HttpResult();
        try{
            teacherService.deleteTeacherByGonghao(gonghao);
        }catch (Exception e){
            result.setStatus("delete fail");
        }finally {
            return result;
        }
    }



    /*
     * 根据教师Id查询教师的所有课程
     * */
//    @GetMapping(value = "/teacher/{teacherId}/courses")
//    public HttpResult queryCoursesByTeacher(@PathVariable Long teacherId){
//        HttpResult<List<Course>> result = new HttpResult<>();
//        result.setData(teacherService.findCoursesByTeacher(teacherId));
//        result.setStatus("ok");
//        return result;
//    }
}
